public class num142 {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode snow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            snow = snow.next;
            if(fast == snow){
                ListNode prev = head;
                while (prev != snow){
                    prev = prev.next;
                    snow = snow.next;
                }
                return prev;
            }
        }
        return null;
    }
}
